package org.dwell.vertx3.demos.consumer;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Context;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.EventBus;

/**
 * Created by JianGuo on 2016/1/18.
 */
public class ConsumerVerticle extends AbstractVerticle {

    private Vertx vertx;

    @Override
    public void start() throws Exception {
        EventBus eb = vertx.eventBus();

        eb.consumer("news.uk.sport", message -> {
            System.out.println("I have received a message: " + message.body());

            message.reply("How interesting!");
        });


        vertx.eventBus().send("news.uk.sport", "Yay! Someone kicked a ball.");

        vertx.eventBus().send("news.uk.sport", "Yay! Someone kicked a ball.", ar -> {
            if(ar.succeeded()) {
                System.out.println("Received Reply: " + ar.result().body());
            }
        });

        DeliveryOptions options = new DeliveryOptions();
        options.addHeader("massage", "yes");
        options.setSendTimeout(3000);
        vertx.eventBus().send("news.uk.sport", "Hey! I'm come again, you big big freak! ", options);
    }

    @Override
    public void init(Vertx vertx, Context context) {
        this.vertx = vertx;
    }


    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new ConsumerVerticle());
    }
}
